package com.lenovo.smarttraffic.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.TrafficNewsBean;

import java.util.List;

/**
 * Created by Caleb.F
 * on 2019-12-03
 * at 14:06
 */
public class MyInfoRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private static final String TAG = "MyInfoRecyclerViewAdapt";
    private List<TrafficNewsBean> mlist;
    private static final Integer TYPE_INMAGE = 0;
    private static final Integer TYPE_COMMON = 1;
    private OnItemClickListener mOnItemClickListener;


    public MyInfoRecyclerViewAdapter(Context context, List<TrafficNewsBean> list) {
        mContext = context;
        mlist = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_COMMON) {
            return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.recycler_info_type, parent, false));
        } else {
            return new MyViewHolderImage(LayoutInflater.from(mContext).inflate(R.layout.recycler_info_type1, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            ((MyViewHolder) holder).mTvInfoTime.setText(mlist.get(position).getCreateTime());
            ((MyViewHolder) holder).mTvInfoTitle.setText(mlist.get(position).getTitle());
            ((MyViewHolder) holder).mTvTitleContent.setText(mlist.get(position).getContent());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.showItem(holder.getAdapterPosition());
                }
            });
        } else if (holder instanceof MyViewHolderImage) {
            Glide.with(mContext).asBitmap().load(mlist.get(position).getImageUrl()).error(R.mipmap.palace).into(((MyViewHolderImage) holder).mImageView20);
            ((MyViewHolderImage) holder).mTvTitleContent.setText(mlist.get(position).getContent());
            ((MyViewHolderImage) holder).mTvInfoTitle.setText(mlist.get(position).getTitle());
            ((MyViewHolderImage) holder).mTvInfoTime.setText(mlist.get(position).getCreateTime());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.showItem(holder.getAdapterPosition());
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
                if (mlist.get(position).getCategory().equals(Constant.TRAFFIC_ITEM_SPOT)) {
                    return TYPE_INMAGE;
                } else {
                    return TYPE_COMMON;
        }
    }

    public void setMlist(List<TrafficNewsBean> list) {
        mlist = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView mTvInfoTitle;
        TextView mTvTitleContent;
        TextView mTvInfoTime;

        public MyViewHolder(View convertView) {
            super(convertView);
            mTvInfoTitle = convertView.findViewById(R.id.tv_info_title);
            mTvTitleContent = convertView.findViewById(R.id.tv_title_content);
            mTvInfoTime = convertView.findViewById(R.id.tv_info_time);

        }
    }


    class MyViewHolderImage extends RecyclerView.ViewHolder {
        TextView mTvInfoTitle;
        TextView mTvTitleContent;
        TextView mTvInfoTime;
        ImageView mImageView20;

        public MyViewHolderImage(View convertView) {
            super(convertView);
            mTvInfoTitle = convertView.findViewById(R.id.tv_info_title);
            mTvTitleContent = convertView.findViewById(R.id.tv_title_content);
            mTvInfoTime = convertView.findViewById(R.id.tv_info_time);
            mImageView20 = convertView.findViewById(R.id.imageView20);

        }
    }


    public interface OnItemClickListener {
        void showItem(int pos);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
